package com.odp.service.impl;

import com.odp.entity.Subscribe;
import com.odp.mapper.SubscribeMapper;
import com.odp.mapper.UserMapper;
import com.odp.service.SubscribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SubscribeServiceImpl implements SubscribeService {
    @Autowired
    private SubscribeMapper subscribeMapper;

    @Autowired
    private UserMapper userMapper;
    @Override
    public Boolean addSubscribe(int id) {
        if(subscribeMapper.checkSubscribe(id,2)!=null){
            return false;//已经关注过了
        }
        Subscribe subscribe = new Subscribe();
        //传入参数
        subscribe.setUserId(id);
        subscribe.setFollowerId(2);//session里取  目前操作的用户关注了别人  他成为了别人的粉丝

        int flag1 = subscribeMapper.addSubscribe(subscribe);//关注表项+1
        int flag2 = userMapper.addFollowers(id);//被关注人的粉丝数+1
        if(flag1>0&&flag2>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean unSubscribe(int id) {
        int flag = subscribeMapper.unSubscribe(id, 2);
        if(flag>0){
            return true;
        }
        return false;
    }
}
